# coding:utf-8
"""
description:Notifications页面
"""
import os
import random

from common.Log import LOG
from pages.Dashboard_page import dashboard_page
from common.Gesture_Mainpulation import gesture_mainpulation
from appium.webdriver.common import mobileby

from common.Get_Yaml import get_yaml
from config.GlobalParameter import TEST_Element_YAML

dataElements = get_yaml(os.path.join(TEST_Element_YAML, "Notifications.yaml"))


class Notifications_page(dashboard_page):
    by = mobileby.MobileBy()

    # 点击右上角的delete按钮
    NotificationsDelete_loc = (by.ID, dataElements.get_elementInfo(0))

    def click_NotificationsDelete(self):
        """ 点击右上角的delete按钮"""
        self.find_element(*self.NotificationsDelete_loc).click()

    # 点击Filter条件，All
    FilterAll_loc = (by.ID, dataElements.get_elementInfo(1))

    def click_FilterAll(self):
        """点击Filter条件，All"""
        self.find_element(*self.FilterAll_loc).click()

    # 点击Filter条件，Systems
    FilterSystems_loc = (by.ID, dataElements.get_elementInfo(2))

    def click_FilterSystems(self):
        """ 点击Filter条件，Systems"""
        self.find_element(*self.FilterSystems_loc).click()

    # 点击Filter条件，Devices
    FilterDevices_loc = (by.ID, dataElements.get_elementInfo(3))

    def click_FilterDevices(self):
        """ 点击Filter条件，Devices"""
        self.find_element(*self.FilterDevices_loc).click()

    # 点击ReadAll
    NotificationsReadAll_loc = (by.ID, dataElements.get_elementInfo(4))

    def click_NotificationsReadAll(self):
        """点击ReadAll"""
        self.find_element(*self.NotificationsReadAll_loc).click()

    # 获取Notification 的read 绿色小图标
    NotificationRead_loc = (by.ID, dataElements.get_elementInfo(7))

    def get_NotificationRead(self):
        """获取Notification 的read 绿色小图标"""
        return self.find_elements(*self.NotificationRead_loc)

    def func_NotifReadAll(self):
        """
        测试点击Read ALl后，没有小绿标显示，都为已读状态
        :return: False
        """
        super().click_DashboardNotifs()  # 点击dashboard页面上的Notification icon
        self.click_NotificationsReadAll()
        gesture_mainpulation().swipe_up(self.driver, self.get_SwipeNotification())
        return self.assertVisible(*self.NotificationRead_loc)

    # 获取Notification Title
    NotificationTitle_loc = (by.ID, dataElements.get_elementInfo(5))

    def get_NotificationTitle(self):
        """获取Notification Title"""
        return self.find_elements(*self.NotificationTitle_loc)

    def assert_NotificationTitle(self):
        """判断Notification Title"""
        return self.assertVisible(*self.NotificationTitle_loc)

    # 获取Notification Timestamp
    NotificationTimestamp_loc = (by.ID, dataElements.get_elementInfo(6))

    def get_NotificationTimestamp(self):
        """获取Notification Timestamp"""
        return self.find_elements(*self.NotificationTimestamp_loc)

    # 删除页面》点击Select all
    NotificationSelectAll_loc = (by.ID, dataElements.get_elementInfo(8))

    def click_NotificationSelectAll(self):
        """ 删除页面》点击Select all"""
        self.find_element(*self.NotificationSelectAll_loc).click()

    # 删除页面》点击Cancel
    NotificationCancel_loc = (by.ID, dataElements.get_elementInfo(9))

    def click_NotificationCancel(self):
        """ 删除页面》点击Cancel"""
        self.find_element(*self.NotificationCancel_loc).click()

    # 删除页面》每个item的左侧的单选框
    cbNotificationDelete_loc = (by.ID, dataElements.get_elementInfo(10))

    def click_cbNotificationDelete(self):
        """ 删除页面》每个item的左侧的单选框"""
        self.find_element(*self.cbNotificationDelete_loc).click()

    # 上下滑动页面notification page
    SwipeNotification_loc = (by.ID, dataElements.get_elementInfo(11))

    def get_SwipeNotification(self):
        """ 上下滑动页面notification page"""
        return self.find_element(*self.SwipeNotification_loc)

    def func_NotifiDelNull(self, messages):
        """
        测试未选择Notification进行删除
        :param messages:
        :return:
        """
        self.click_NotificationsDelete()  # 点击删除按钮，进入删除页面
        self.click_NotificationsDelete()  # 不选择再次点击删除按钮，进行删除
        return self.find_toast(messages)

    def func_NotifiDelSome(self, loopN=2):
        """
        测试随机选择几个Notification，点击删除》点击弹框取消》再次进行删除
        :param loopN: 循环执行次数
        :return:
        """
        for i in range(loopN):  # 有相同部分使用循环两次，第一次删除取消，第二次确认删除
            self.click_NotificationsDelete()
            num = random.randint(1, 4)
            print(num, len(self.get_NotificationTitle()))
            self.get_NotificationTitle()[num].click()  # 点击随机索引号后选中
            onlytime = self.get_NotificationTimestamp()[num].text  # 获取随机索引号后的文字
            print(onlytime)
            self.click_NotificationsDelete()  # 再次点击删除按钮才能进行删除
            if i == 0:
                super().click_btnDeletePopupCancel()
                LOG.info("随机点击一个后删除并取消，之前选中item存在,%s>>>" % self.find_toast(onlytime))
                self.click_NotificationCancel()  # 退出删除页面
            else:
                super().click_btnDeletePopupDelete()  # 确认弹框删除
                return self.find_toast(onlytime)

    def func_NotifiDelAll(self, messages):
        """
        随机切换system和device后删除，在返回到all下，没有数据
        :param messages:
        :return:
        """
        self.click_NotificationsDelete()
        if random.randint(1, 2):
            self.click_FilterSystems()  # 点击Systems下进行全选删除
        else:
            self.click_FilterDevices()  # 点击Systems下进行全选删除
        self.click_NotificationSelectAll()
        self.click_NotificationsDelete()  # 再次点击删除按钮才能进行删除
        super().click_btnDeletePopupDelete()  # 确认删除
        return self.find_toast(messages)
